import * as turf from '@turf/turf'
/**
 * 计算缩放程度
 * @param e
 * @returns {number}
 */
export const HTgetZoom = (e) => {
  let zoom = 9
  switch (e.grade) {
    case '2':
      zoom = 10
      break
    case '3':
      zoom = 12
      break
    case '4':
      zoom = 13
      break
    case '5':
      zoom = 16
      break
    case '5.5':
      zoom = 17
      break
    case '6':
      zoom = 18
      break
  }
  return zoom
}
export const HTgetCenter = (e, _this) => {
  if (e.csc_mapjson_ht) {
    try {
      let x = e.csc_mapjson_ht.split(',')
      e.lat = x[0]
      e.lng = x[1]
      if (e.lng === '0' || e.lat === '0') {
        _this.$Notice.error({ title: '坐标检验异常！1', desc: '坐标数据异常，将使用默认坐标展示！' })
        return [41.803408523469855, 123.40643097319376]
      }
      return [e.lat, e.lng]
      // let result = checkLngLat(e.lng, e.lat)
      // if (result) {
      //   return [e.lng, e.lat]
      // } else {
      //   _this.$Notice.error({ title: '坐标检验异常！2', desc: '坐标数据异常，将使用默认坐标展示！' })
      //   return [41.805853, 123.412581]
      // }
    } catch (e) {
      _this.$Notice.error({ title: '坐标解析异常！3', desc: '坐标数据异常，将使用默认坐标展示！' })
      return [41.803408523469855, 123.40643097319376]
    }
  } else {
    return [41.803408523469855, 123.40643097319376]
  }
}
export const HTautoCenterTwo = (e, _this) => {
  try {
    let path = JSON.parse(e)
    let total_lng = 0.0
    let total_lat = 0.0
    path.forEach(arr => {
      total_lng += arr[1]
      total_lat += arr[0]
    })
    let newlng = total_lng / path.length
    let newlat = total_lat / path.length
    return [newlat, newlng]
  } catch (e) {
    console.log(e)
    _this.$Notice.error({ title: '坐标解析异常！', desc: '坐标数据异常!' })
  }
}
/**
 * 判断点是否在多边形内
 * @param point
 * @param polygon
 * @returns {boolean}
 * @constructor
 */
export const HTpointAndPolygon = (point, polygon) => {
  var pt = turf.point(point)
  var poly = turf.polygon(polygon)
  return turf.booleanPointInPolygon(pt, poly)
}
